package com.ws.shop.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.stereotype.Repository;

import com.ws.shop.bean.Page;
import com.ws.shop.dao.TransactionDao;
import com.ws.shop.entity.Transaction;
import com.ws.shop.util.JdbcUtil;

@Repository(value="transactionDao")
public class TransactionDaoImpl extends BaseDaoImpl implements TransactionDao {
	private static Logger log = Logger.getLogger(TransactionDaoImpl.class);
	final String findTransactionListPage_count = "select count(1) from jvf_transaction";
	final String findTransactionListPage_list = "select id,uid,name,payment,money,addtime,remark from jvf_transaction";
	final String findTransactionListPage_order = " order by id desc";
	public Page findTransactionListPage(int pageno, int pagesize, String name,
			String starttime, String endtime) {
		StringBuilder query = new StringBuilder(" where 1=1");
		if(name != null){
			query.append(" and name='").append(name).append("'");
		}
		query.append(" and addtime<='").append(endtime).append(" 23:59:59'")
			.append(" and addtime>='").append(starttime).append(" 00:00:00'");
		return this.findPage(pageno, pagesize, new StringBuilder(findTransactionListPage_count).append(query).toString(),
				new StringBuilder(findTransactionListPage_list).append(query).append(findTransactionListPage_order));
	}

	@SuppressWarnings("unchecked")
	public void displayUserTransaction(final Page page, final long uid) {
		this.execute(new ConnectionCallback() {
			public Object doInConnection(Connection connect) {
				ResultSet rs = null;
				try {
					Statement stmt = null;
					try {
						StringBuilder query = new StringBuilder("SELECT COUNT(1) FROM jvf_transaction where 1=1 ");
						query.append(" and uid=").append(uid);
						stmt = connect.createStatement();
						rs = stmt.executeQuery(query.toString());
						// System.out.println(sb.toString());
						long totalnum = 0;
						if (rs.next()) {
							totalnum = rs.getLong(1);
							page.setTotalnum(totalnum);
						}
						JdbcUtil.closeResultSet(rs);

						if (totalnum > 0) {
							StringBuilder sb = new StringBuilder(
									"SELECT id, uid,NAME,payment,money,ADDTIME,beat,remark  FROM  jvf_transaction WHERE 1=1");
							rs = stmt.executeQuery(sb.append(
									" order by addtime desc limit ").append(
									page.getStartrownum()).append(",").append(
									page.getPagesize()).toString());
							// System.out.println(sb.toString());
							Transaction tran = null;
							while (rs.next()) {
								tran = new Transaction();
								tran.setId(rs.getLong(1));
								tran.setUid(rs.getInt(2));
								tran.setName(rs.getString(3));
								tran.setPayment(rs.getString(4));
								tran.setMoney(rs.getInt(5));
								tran.setAddtime(rs.getTimestamp(6));
								tran.setBeat(rs.getInt(7));
								tran.setRemark(rs.getString(8));
								page.addData(tran);
							}
						}
					} catch (Exception e) {
						log.error(e.getMessage(), e);
					} finally {
						JdbcUtil.close(rs, stmt, null);
					}
					return null;

				} catch (Exception e) {
					e.printStackTrace();
					log.error(" displayUserPointc!", e);
					return null;
				}
			}
		});
	}
}
